package com.base.cn.platform.os.controller.mobile.order.user;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.order.PayType;
import com.base.cn.platform.os.entity.order.order.OrderCondition;
import com.base.cn.platform.os.service.manage.order.goods.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户订单Controller
 *
 * @author s.li
 * @create 2018-06-08-9:58
 */
@Controller
@RequestMapping("/mobile/uc/order")
public class MobileUserOrderController extends MobileBaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户订单列表
     * @return
     */
    @RequestMapping("/orderList")
    public String userOrderList(){
        return resultPage("/order/user/user-order-list");
    }

    /**
     * 加载订单数据
     * @param model
     * @param request
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxUserOrderList")
    public String ajaxUserOrderList(Model model, HttpServletRequest request,
                                    @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("status",1);
        params.put("notPayType","'"+ PayType.PAY_TYPE_FREE +"','"+PayType.PAY_TYPE_COURSE_CART+"','"+PayType.PAY_TYPE_MEMBER+"'");
        Map<String,Object> dataMap = orderService.findWebOrderPage(params,currentPage,5,false,true,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/order/user/ajax-user-order-list");
    }

    /**
     * 用户订单详情
     * @return
     */
    @RequestMapping("/orderDetail")
    public String userOrderDetail(Model model,
                                  @RequestParam("orderId") BigDecimal orderId){
        OrderCondition condition = new OrderCondition();
        condition.setId(orderId);
        condition.setSetUserData(true);
        condition.setSetDetailsData(true);
        condition.setSetShopData(true);
        condition.setSetOrderBook(false);
        condition.setSetActivity(true);
        condition.setSetCoupon(false);
        Map<String,Object> order = orderService.findOrderInfo(condition);
        model.addAttribute("order",order);
        return resultPage("/order/user/user-order-detail");
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @RequestMapping("/cancelOrder")
    @ResponseBody
   public Map<String,Object> cancelOrder(@RequestParam("orderId") BigDecimal orderId){
     return    orderService.cancelOrder(orderId);
    }
}
